package com.mmf.ancientcostume.base.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.mmf.ancientcostume.common.utils.ListUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by MMF on 2017-11-06.
 */

public abstract class MyBaseRecyclerAdapter<T extends RecyclerView.ViewHolder, E> extends RecyclerView.Adapter<T> {
    private static OnItemClickListener onItemClickListener;
    private static OnItemLongClickListener onItemLongClickListener;

    public static interface OnItemClickListener<E> {
        void onItemClick(View view, E bean);
    }

    public static interface OnItemLongClickListener<E> {
        void onItemLongClick(View view, E bean);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void seOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    protected Context context;
    protected List<E> itemList = new ArrayList<>();

    public MyBaseRecyclerAdapter(Context context) {
        this.context = context;
    }

    @Override
    public T onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = View.inflate(context, getLayout(), null);
        return getViewHolder(itemView);
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    @Override
    public void onBindViewHolder(final T holder, final int position) {
        final E item = itemList.get(position);
        setValues(holder, item);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(holder.itemView, item);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemLongClickListener != null) {
                    onItemLongClickListener.onItemLongClick(holder.itemView, item);
                }
                return true;
            }
        });
    }

    /**
     * 在原有的数据上添加新数据
     *
     * @param itemList
     */
    public void addItems(List<E> itemList) {
        this.itemList.addAll(itemList);
        notifyDataSetChanged();
    }

    /**
     * 设置为新的数据，旧数据会被清空
     *
     * @param itemList
     */
    public void setItems(List<E> itemList) {
        this.itemList.clear();
        if (!ListUtils.isEmpty(itemList)) {
            this.itemList = itemList;
        }
        notifyDataSetChanged();

    }

    /**
     * 获取item的界面布局
     *
     * @return
     */
    protected abstract int getLayout();

    /**
     * 返回viewholder
     *
     * @param itemView
     * @return
     */
    protected abstract T getViewHolder(View itemView);

    /**
     * 设置控件数据
     *
     * @param holder
     * @param bean
     */
    protected abstract void setValues(T holder, E bean);


}
